package com.atguigu.crowd.handler;

import com.atguigu.crowd.constant.CrowdContant;
import com.atguigu.crowd.entity.po.MemberPO;
import com.atguigu.crowd.service.api.MemberService;
import com.atguigu.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

@RestController
public class MemberHandler {

    @Autowired
    private MemberService memberService;

    @RequestMapping("/save/member/remote")
    public ResultEntity<String> saveMember(@RequestBody MemberPO memberPO){
        try {
            memberService.saveMember(memberPO);

            return ResultEntity.successWithoutData();

        }catch (Exception e){
          //检查账号的唯一性
            if (e instanceof DuplicateKeyException){
                return ResultEntity.failed(CrowdContant.MESSAGE_LOGIN_ACCT_ALREADY_IN_USE);
            }
            return ResultEntity.failed(e.getMessage());

        }

    }
//    @RequestMapping("")
//    @ResponseBody
//    public ResultEntity<MemberPO> getMemberByloginAcct(){
//
//        return null;
//    }

    @RequestMapping("/get/memberpo/by/login/acct/remote")
    public ResultEntity<MemberPO> getMemberPOByLoginAcctRemote(@RequestParam("loginAcct") String loginAcct){
       try{
           MemberPO memberPO= memberService.getMemberPOByLoginAcct(loginAcct);

           return ResultEntity.successWithoutData(memberPO);

       }catch (Exception e){
           e.printStackTrace();
           return ResultEntity.failed(e.getMessage());
       }
    }


}
